import { Modal } from 'antd';
import { axGet, axPostJson } from '../../lib/request';
import urls from './basic.url';
import { downloadFile } from '../../lib/util';

const basicService = {
  getCoreEnterpriseList(data) {
    /**
     *  获取核心企业列表
     *  */
    return axPostJson(urls.coreEnterpriseList, data, null, { noLoading: true });
  },
  addCoreEnterprise(data) {
    /**
     *  新增核心企业
     *  */
    return axPostJson(urls.addCoreEnterprise, data);
  },
  updateCoreEnterprise(data) {
    /**
     *  更新核心企业
     *  */
    return axPostJson(urls.updateCoreEnterprise, data);
  },
  getDealerList(data) {
    /**
     *  获取经销商列表
     *  */
    return axPostJson(urls.dealerList, data, null, { noLoading: true });
  },
  addDealer(data) {
    /**
     *  新增经销商
     *  */
    return axPostJson(urls.addDealer, data);
  },
  updateDealer(data) {
    /**
     *  更新经销商信息
     *  */
    return axPostJson(urls.updateDealer, data);
  },
  getDealerPurchaseList(data) {
    /**
     *  获取经销商采购信息列表
     *  */
    if (!data.purchaseId) {
      return Promise.resolve({
        success: false,
      });
    }
    return axPostJson(
      urls.dealerPurchaseList,
      {
        cgdCustId: data.purchaseId,
        pageNum: data.pageNum,
        pageSize: data.pageSize,
      },
      null,
      { noLoading: true },
    );
  },
  addDealerPurchase(data) {
    /**
     *  新增经销商采购信息
     *  */
    return axPostJson(urls.addDealerPurchase, data);
  },
  updateDealerPurchase(data) {
    /**
     *  更新经销商采购信息
     *  */
    return axPostJson(urls.updateDealerPurchase, data);
  },
  deleteDealerPurchase(id) {
    /**
     *  删除一条经销商采购
     *  */
    return axGet(urls.deleteDealerPurchase(id), null, { method: 'delete' });
  },
  deleteDealer(id) {
    /**
     *  删除一条经销商
     *  */
    return axGet(urls.deleteDealer(id), null, { method: 'delete' });
  },
  downloadDealerTemplate() {
    /**
     *  下载模板
     *  */
    return axGet(urls.dealerTemplate, null, {
      noToast: true,
      responseType: 'blob',
      returnResponse: true,
    }).then((res) => {
      if (res.data && res.data.type !== 'application/json') {
        downloadFile(res.data, res.headers['content-disposition']);
      } else {
        const reader = new FileReader();
        reader.onload = (text) => {
          const result = text.target.result;
          if (typeof result === 'string') {
            const resultJson = JSON.parse(result);
            if (resultJson.code !== '1') {
              Modal.error({
                title: '提示',
                content: resultJson.msg,
              });
            }
          }
        };
        reader.readAsText(res);
      }
      return true;
    });
  },
  getBankOrderList(data) {
    /**
     *  获取订单列表
     *  */
    return axPostJson(urls.bankOrder, data, null, { noLoading: true });
  },
};

export default basicService;
